<?php

namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;

class MallOrder extends Base
{
    use SoftDeletes;

    const STATUS_WAIT_PAY = 0;
    const STATUS_WAIT_PRODUCT = 1;
    const STATUS_DELIVER_PRODUCT = 2;
    const STATUS_FINISH = 3;
    const STATUS_CLOSE = 4;

    const PAY_TYPE_WX = 2;
    const PAY_TYPE_ALI = 1;

    const CLOSE_TYPE_USER = 1;
    const CLOSE_TYPE_ADMIN = 2;
    const CLOSE_TYPE_SYSTEM = 3;


    protected $appends = ['origin_amount_yuan', 'paid_amount_yuan', 'fare_yuan'];

    public function order_product() {
        return $this->hasMany(MallOrderProduct::class, 'order_id')->where('is_zero', 0);
    }

    public function order_product_zero() {
        return $this->hasMany(MallOrderProduct::class, 'order_id')->where('is_zero', 1);
    }

    public function getOriginAmountYuanAttribute() {
        return format_yuan($this->origin_amount);
    }

    public function getPaidAmountYuanAttribute() {
        return format_yuan($this->paid_amount);
    }

    public function getFareYuanAttribute() {
        return format_yuan($this->fare);
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function address() {
        return $this->belongsTo(AddressSnapshot::class, 'address_snapshot_id');
    }
}
